package com.cndym.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 基本json转换,主要应用于oracle存储过程中批量数据输出
 */
public class Jsion {

    public static String callback(String callback,String body){
        return callback+"("+body+")";
    }


    public static String listToJson(List<Map<String, Object>> list) {
        Map<String, Object> map = null;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            String temp = toJson(map);
            if (null != temp && temp.length() > 1) {
                stringBuffer.append("," + temp);
            }
        }
        if (stringBuffer.length() > 1) {
            return "[" + stringBuffer.substring(1) + "]";
        }
        return null;
    }

    public static String mapToJson(Map<String, Object> map) {
        String temp = toJson(map);
        if (null != temp && temp.length() > 1) {
            return "[" + temp + "]";
        }
        return null;
    }

    private static String toJson(Map<String, Object> map) {
        Iterator<String> it = map.keySet().iterator();
        StringBuffer temp = new StringBuffer();
        while (it.hasNext()) {
            String key = it.next().toString();
            String value = map.get(key).toString();
            value = value.replaceAll("\"", "");
            value = value.replaceAll("\r","");
            value = value.replaceAll("\n","");
            temp.append(",\"" + key + "\":\"" + value + "\"");
        }
        if (temp.length() > 1) {
            return "{" + temp.substring(1) + "}";
        }
        return null;
    }

    public static List<Map<String, Object>> jsonToList(String args) {
        List<String> list = new ArrayList<String>();
        analysis(args, list);
        List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
        Map<String, Object> temp = new HashMap<String, Object>();
        if (null != list && (list.size() % 2) == 0) {
            for (int i = 0; i < list.size(); i = i + 2) {
                if (temp.containsKey(list.get(i))) {
                    maps.add(temp);
                    temp = new HashMap<String, Object>();
                }
                temp.put(list.get(i), list.get(i + 1));
            }
            maps.add(temp);
        }
        return maps;
    }

    public static Map<String, Object> jsonToMap(String str) {
        List<Map<String, Object>> list = jsonToList(str);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public static List<String> analysis(String string, List<String> list) {
        String tag = "\"";
        if (null != string) {
            int index = string.indexOf(tag) + 1;
            string = string.substring(index);
            index = string.indexOf(tag);
            String temp = string.substring(0, index);
            list.add(temp);
            string = string.substring(index + 1);
            index = string.indexOf(tag);
            if (index > -1) {
                analysis(string, list);
            }
        }
        return list;
    }

    public static List<String> findStringsByRegx(String regex, String Content, int index) {
        List<String> stringList = new ArrayList<String>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(Content);
        while (matcher.find()) {
            stringList.add(matcher.group(index));
        }
        return stringList;
    }

    public static Object jsonToObject(Object object, String str) {
        Map<String, Object> map = jsonToMap(str);
        return mapToObject(object, map);
    }

    public static String objectToJson(Object object) {
        if (null == object) {
            return "";
        }
        Map<String, Object> map = objectToMap(object);
        return mapToJson(map);
    }

    public static Object mapToObject(Object object, Map<String, Object> map) {
        Class classes = object.getClass();
        try {
            Object subObject = classes.newInstance();
            Method[] methods = classes.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];
                if (method.getName().indexOf("set") == 0) {
                    Object temp = map.get(methodRealName(method.getName()));
                    if (null != temp) {
                        method.invoke(subObject, new Object[]{temp});
                    }
                }
            }
            return subObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String ObjectsToMapsToJson(List list) {
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> map = objectToMap(list.get(i));
            mapList.add(map);
        }
        return listToJson(mapList);
    }

    public static Map<String, Object> objectToMap(Object object) {
        Map<String, Object> map = new HashMap<String, Object>();
        Class classes = object.getClass();
        try {
            Map<String, Method> methodName = new HashMap<String, Method>();
            Method[] methods = classes.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];
                if (method.getName().indexOf("get") == 0) {
                    methodName.put(methodRealName(method.getName()), method);
                }
            }
            Field[] fields = classes.getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field field = fields[i];
                if (methodName.containsKey(field.getName())) {
                    Method method = methodName.get(field.getName());
                    Object out = method.invoke(object, null);
                    if (null == out) {
                        out = "";
                    }
                    map.put(field.getName(), out.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    private static String methodRealName(String str) {
        if (str.length() > 3) {
            str = str.substring(3, str.length());
            str = str.substring(0, 1).toLowerCase() + str.substring(1);
        }
        return str;
    }

    public static String version() {
        return "version 0.0.0.1 running jdk 1.2 or later";
    }

}
